package com.haizhi.script;

import com.haizhi.redis.RedisHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: youfeng
 * Date: 2018-06-22
 * Time: 15:29
 */
public class RedisClient {
    private static final Logger logger = LogManager.getLogger(RedisClient.class);

    private RedisHandler redisHandler;

    private RedisClient() {


        Properties props = new Properties();
        try {
            props.load(RedisHandler.class.getResourceAsStream("/redis.properties"));
        } catch (IOException e) {
            logger.error("初始化配置文件异常:", e);
        }

        //创建jedis池配置实例
        JedisPoolConfig config = new JedisPoolConfig();

        //设置池配置项值
        config.setMaxTotal(Integer.valueOf(props.getProperty("redis.pool.maxTotal")));
        config.setMaxIdle(Integer.valueOf(props.getProperty("redis.pool.maxIdle")));
        config.setMinIdle(Integer.valueOf(props.getProperty("redis.pool.minIdle")));
        config.setMaxWaitMillis(Long.valueOf(props.getProperty("redis.pool.maxWaitMillis")));
        config.setTestOnBorrow(Boolean.valueOf(props.getProperty("redis.pool.testOnBorrow")));
        config.setTestOnReturn(Boolean.valueOf(props.getProperty("redis.pool.testOnReturn")));

        //根据配置实例化jedis池
        String password = props.getProperty("redis.password", null);
        redisHandler = new RedisHandler(config, props.getProperty("redis.host"),
                Integer.valueOf(props.getProperty("redis.port")),
                Integer.valueOf(props.getProperty("redis.timeout")),
                password,
                Integer.valueOf(props.getProperty("redis.db", "0")));
        logger.info("redis 初始化成功...");
    }

    private String getValue(String key) {
        Jedis jedis = redisHandler.getJedisObject();
        String result = jedis.get("bc4299f0f9bb5b19cb24c1306c2a84d5");
        redisHandler.returnJedisOjbect(jedis);
        return result;
    }

    public static void main(String... args) {
        logger.info(new RedisClient().getValue("bc4299f0f9bb5b19cb24c1306c2a84d5"));
    }

}
